/**
 *
 */
package com.pos.net.netparam;

import android.content.Context;
import android.os.Build;

import com.pos.modules.bean.UserBean;
import com.pos.set.AppSet;
import com.pos.set.Log;
import com.pos.toolbox.Tools;
import com.pos.toolbox.file.FilesManager;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author cai
 * @description:网络请求参数
 * @time:2017年1月9日上午12:35:21
 */
public class NetParams {
    /**
     * 基础map
     */
    public static double longitude;
    public static double latitude;
    private static HashMap<String, Object> map;

    // private static String encodeParameters(Map<String, Object> params) {
    // if (params == null)
    // return null;
    // String paramsEncoding = "UTF-8";
    // StringBuilder encodedParams = new StringBuilder();
    // try {
    // Iterator<Entry<String, Object>> iter = params.entrySet().iterator();
    // while (iter.hasNext()) {
    // Map.Entry<String, Object> entry = iter.next();
    // encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
    // encodedParams.append('=');
    // encodedParams.append(URLEncoder.encode(entry.getValue() + "",
    // paramsEncoding));
    // if (iter.hasNext()) {
    // encodedParams.append('&');
    // }
    // }
    // Log.out("request data", encodedParams.toString());
    // return encodedParams.toString();
    // } catch (UnsupportedEncodingException uee) {
    // throw new RuntimeException("Encoding not supported: " + paramsEncoding,
    // uee);
    // }
    // }
    private static String encodeParameters(Map<String, Object> params) {
        if (params == null)
            return null;
        StringBuilder encodedParams = new StringBuilder();
        Iterator<Entry<String, Object>> iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, Object> entry = iter.next();
            encodedParams.append(URLEncoder.encode(entry.getKey()));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode(entry.getValue() + ""));
            if (iter.hasNext()) {
                encodedParams.append('&');
            }
        }
        Log.out("request data", encodedParams.toString());
        android.util.Log.e("TAG", "encodeParameters: " + encodedParams.toString());
        return encodedParams.toString();

    }

    /**
     * 退货
     *
     * @param orderCode
     */
    public static String posSaleReturn(Context context, String orderCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderCode", orderCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 退款订单详情
     *
     * @param orderCode
     */
    public static String posQuerySalereturnOrderDetail(Context context, String orderCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderCode", orderCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 填充完整信息
     * <p>
     * 请求的type
     */
    private static HashMap<String, Object> getBasicMap(Context context) {

        if (map == null) {
            map = new HashMap<String, Object>();
        } else {
            map.clear();
        }
        map.put("version", AppSet.getVersionName(context));
        // 软件id号
        // map.put("softId", 1);
        map.put("softId", 30);
        // map.put("softId", 11);
        map.put("spCode", 0);
        map.put("os", "AndroidPOS");
        map.put("deviceType", Build.MODEL);
        map.put("longitude", longitude + "");
        map.put("latitude", latitude + "");

        return map;
    }

    /**
     * 检测新版本
     */
    public static String checkNewVersion(Context context) {
        android.util.Log.e("TAG", "posUserLogin: " + encodeParameters(getBasicMap(context)));
        return encodeParameters(getBasicMap(context));
    }

    /**
     * 登录
     */
    public static String posUserLogin(Context context, String userAccount, String userPassword) {
        HashMap<String, Object> map = getBasicMap(context);
        map.put("userAccount", userAccount);
        map.put("userPassword", Tools.encodeMD5(userPassword).toUpperCase());

        return encodeParameters(map);
    }

    /**
     * 查询商品信息
     */
    public static String posQueryGoodsByCode(Context context, String goodsCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("goodsCode", goodsCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 商品单品库存查询
     */
    public static String posGetGoodsStock(Context context, String goodsCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("goodsCode", goodsCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 商品单品库存上传
     */
    public static String posSetGoodsStock(Context context, String goodsCode, String goodsStock) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("goodsCode", goodsCode);
        map.put("goodsStock", goodsStock);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 生成订单
     */
    public static String posCreateOrder(Context context, String goodsCodes, String memberPayToken) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("goodsCodes", goodsCodes);
        map.put("memberPayToken", memberPayToken);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 生成订单(无商品)
     */
    public static String posCreateOrderWithoutGoods(Context context, String orderAmount) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderAmount", orderAmount);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 刷新会员价格和会员支付信息
     */
    public static String posQueryVIPInfos(Context context, String orderId, String memberCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderId", orderId);
        map.put("memberCode", memberCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 查询会员信息
     */
    public static String posQueryMember(Context context, String memberCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("memberCode", memberCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 支付通知
     *
     * @param payStatus       0成功，1失败
     * @param payMethod       0现金，1微信，2支付宝，3翼支付，4银行卡
     * @param payBillNumber   现金支付时为空
     * @param payDate         yyyy-MM-dd HH:mm:ss
     * @param payAmount       第三方支付金额
     * @param cashAmount      现金支付金额 @ cashChange 找零金额
     * @param memberPayToken  会员token
     * @param secondPayNumber 三方支付序列号，当payMethod为1,2,3时，取PayContacts.SERVERAUDITNUMBER，为4时，
     *                        取PayContacts.REFERENCE_NO
     * @param merCode         银行卡交易有效，取PayContacts.MERCHANT_ID
     * @param merName         银行卡交易有效，取PayContacts.MERCHANT_NAME
     * @param termCode        银行卡交易有效，取PayContacts.TERMINAL_ID
     * @param bankName        银行卡交易有效，取PayContacts.BANK_CODE_ID
     * @param bankCard        银行卡交易有效，取PayContacts.CARD_NUMBER
     * @param bankCardExpired 银行卡交易有效，取PayContacts.CARD_VALIDITY
     * @param authorCode      银行卡交易有效，取PayContacts.AUTHORIZATION_NUMBER
     * @param tradeBatchCode  银行卡交易有效，取PayContacts.BATCH_NUMBER
     */
    public static String posPayOrderSuccess(Context context, String orderId, Object payStatus, Object payMethod,
                                            String payBillNumber, String payDate, Object payAmount, Object cashAmount, Object cashChange,
                                            String memberPayToken, String secondPayNumber, String merCode, String merName, String termCode,
                                            String bankName, String bankCard, String bankCardExpired, String authorCode, String tradeBatchCode, String orderCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderId", orderId);
        map.put("payStatus", payStatus);
        map.put("payMethod", payMethod);
        map.put("payBillNumber", payBillNumber);
        map.put("payDate", payDate);
        map.put("payAmount", payAmount);
        map.put("cashAmount", cashAmount);
        map.put("cashChange", cashChange);
        map.put("memberPayToken", memberPayToken);
        map.put("token", userBean.getToken());
        map.put("secondPayNumber", secondPayNumber);
        map.put("merCode", merCode);
        map.put("merName", merName);
        map.put("termCode", termCode);
        map.put("bankName", bankName);
        map.put("bankCard", bankCard);
        map.put("bankCardExpired", bankCardExpired);
        map.put("authorCode", authorCode);
        map.put("tradeBatchCode", tradeBatchCode);
        String sn = android.os.Build.SERIAL;
        Log.e("sn", "设备sn=" + sn);
        map.put("termSn", sn);
        map.put("orderCode", orderCode);
        return encodeParameters(map);
    }

    /**
     * 交易记录查询
     *
     * @param orderDate 订单日期 yyyy-MM-dd
     */
    public static String posQueryOrders(Context context, String orderDate, String orderStatus) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderDate", orderDate);
        map.put("orderStatus", orderStatus);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 订单详情
     *
     * @param orderCode
     */
    public static String posQueryOrderDetail(Context context, String orderCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderCode", orderCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

    /**
     * 补打印小票
     *
     * @param orderCode
     */
    public static String posGetOrderPrintInfo(Context context, String orderCode) {
        HashMap<String, Object> map = getBasicMap(context);
        UserBean userBean = (UserBean) FilesManager.readObject(context, FilesManager.User);
        map.put("orderCode", orderCode);
        map.put("token", userBean.getToken());
        return encodeParameters(map);
    }

}
